Make your own free website on Tripod.com

This page has been superceded by a wiki version of this example: ClassExample





import std.c.stdio;





class Television

{ 

    

    void pressPowerButton()

    {

        if(m_turnedOn)

            printf("It was on, but now you turned it off.\n");  

        else

            printf("Now, it's turned on (channel %d).\n", m_channel);

        m_turnedOn = !m_turnedOn;

    }

    

    void changeChannel(int c)

    {  

        if(m_turnedOn)

        {

            if(c == m_channel)

                printf("It's already on channel %d, silly.\n", m_channel);

            else

            { 

                printf("Per your request, it's now on channel %d.\n", m_channel);

                m_channel = c;

            }

        }

        else

            printf("Hmm... the TV's off. Try turning it on before you pick a channel.\n", m_channel);

                

    }

    

    this()

    {

        m_turnedOn = false;  

        m_channel = 3;

        printf("\n(Creating this object is kind of like plugging in a TV set.)\n\n");

    }

    

    ~this()

    { 

        printf("\n(Destroying this object is kind of like unplugging a TV set.)\n\n");

    }

    

    private

    {  

        bit m_turnedOn;

        int m_channel;

    }

}





void main()

{ 

    

    Television BigScreen = new Television();

    

    BigScreen.pressPowerButton();

    BigScreen.changeChannel(44);

    BigScreen.changeChannel(49);

    BigScreen.changeChannel(49);

    BigScreen.pressPowerButton();

    BigScreen.changeChannel(34);

    BigScreen.pressPowerButton();

    BigScreen.changeChannel(34);

    BigScreen.changeChannel(49);

    BigScreen.pressPowerButton();

    

    delete BigScreen;

}